from pathlib import Path
from typing import TextIO
from game.event.eventManager import EventId, Eventbus

ASSET_DIR = Path(__file__).parent.absolute()


class Sentence:
    def __init__(self, name: str, sentence: str):
        self.name = name
        self.sentence = sentence


class Paragraph:  # 2types, has choices or doesn't;
    # if no choice,len(choices) == 0; else: len(choices_text) == len(choices)
    def __init__(self, choices: list[str], choices_text: list[list[Sentence]]):
        self.choices = choices
        self.choice_index = 0
        self.choices_text = choices_text
        self.current_choice = self.choices_text[self.choice_index]
        self.current_sentence_index = 0
        self.current_sentence = self.current_choice[self.current_sentence_index]


class Dialog:
    def __init__(self, dialog_file: TextIO):

        self.paragraphs = []  # type: list[Paragraph]

        dialog_text = dialog_file.read().split('\n\n')
        for paragraph in dialog_text:

            sentences = paragraph.split('\n')
            Sentences = []  # type:list[Sentence]
            choices_text = []  # type:list[list[Sentence]]
            choices = []  # type:list[str]
            choices_indexs = []  # type: list[int]

            for sentence in sentences:
                name = ''
                if sentence[0] == '#':  # 选项
                    sen = sentence.replace('#', '')
                    choices.append(sen)
                    choices_indexs.append(sentences.index(sentence))

                elif ':' in sentence:  # 人物名称
                    sen = sentence.split(':')
                    name = (sen[0])
                    sentence = sen[1]
                Sentences.append(Sentence(name, sentence))

            if len(choices):
                for choice_index in choices_indexs:
                    if choices_indexs[-1] == choice_index:  # 是最后一个选项，则切片到最后
                        choices_text.append(Sentences[choice_index + 1::])
                    else:  # 不是，则将两个选项中间的部分切片
                        choices_text.append(Sentences[choice_index + 1:choices_indexs
                        [choices_indexs.index(choice_index) + 1]:])
            else:
                choices_text.append(Sentences)

            self.paragraphs.append(Paragraph(choices, choices_text))
            self.current_paragraph_index = 0
            self.current_paragraph = self.paragraphs[self.current_paragraph_index]

    def get_current(self):
        self.current_paragraph = self.paragraphs[self.current_paragraph_index]
        self.current_paragraph.current_sentence = self.current_paragraph.current_choice[self.current_paragraph.current_sentence_index]
        self.current_paragraph.current_choice = self.current_paragraph.choices_text[self.current_paragraph.choice_index]
        return self.current_paragraph, self.current_paragraph.current_sentence

    def reset_dialog(self):
        self.current_paragraph_index = 0
        self.current_paragraph.current_sentence_index = 0
        self.get_current()

    def get_next(self):
        print(self.current_paragraph_index)
        # if there is sentence left in the paragraph
        if self.current_paragraph.current_sentence_index < len(self.current_paragraph.current_choice) - 1:
            self.current_paragraph.current_sentence_index += 1
        else:
            # if there is paragraph left in the dialog
            if self.current_paragraph_index < len(self.paragraphs) - 1:
                if len(self.paragraphs[self.current_paragraph_index+1].choices):
                    return self.paragraphs[self.current_paragraph_index+1].choices
                self.current_paragraph.current_sentence_index = 0
                self.current_paragraph_index += 1

            else:
                return 0

        self.get_current()


        


def load_text(asset_path: str):
    return open(ASSET_DIR / asset_path, 'r', encoding='utf8')
